Lab 06 - Docker
Docker
Installation
May not be required in the laboratory.
Remove older versions of Docker:
sudo apt remove docker docker-engine docker.io containerd runc
Update and install required packages:
sudo apt update sudo apt install ca-certificates curl gnupg lsb-release
Add Docker’s official GPG key:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc
Set up the stable Docker repository:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update
Install Docker Engine, CLI, and Containerd:
sudo apt update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify that Docker Engine is installed correctly by running the
hello-world
image:sudo docker run hello-world
(Optional) To run Docker commands without
sudo
, create thedocker
group and add your user to it:sudo groupadd docker sudo usermod -aG docker $USER newgrp docker
You can test if the above step was successful by running:
docker run hello-world
^ Notice that you don’t need
sudo
in front of the command.
The above steps and further documentation are also available here.
Docker Commands
List Docker images
docker images
List all Docker containers
docker ps -a
Start a container
docker start <container_name>
Run an
interactive bash
shell inside a container
docker exec -it <container_name> bash
Create
a new container and run an interactive bash
shell inside
it
docker run -it <image_name> bash
Copy a file from the host to a container
docker cp path/to/file/on/host <container_name>:path/to/file/in/container
Copy a file from a container to the host
docker cp <container_name>:path/to/file/in/container path/to/file/on/host
List volumes
docker volume ls
Create a volume
docker volume create my_volume
Remove a volume
docker volume rm my_volume
🛠🔥 Task 1 🛠🔥
Read Docker’s get started (10 parts in total).
🛠🔥 Task 2 🛠🔥
Containerize the application from the previous class. The application should use bind mounts to write to a file located in the host’s filesystem.
- Hint 1: You can use the Python image as a base for your application image.
- Hint 2: You can install required packages (in this case
click
) globally withpip
during the image build process.
Run the application and check the logs. Try enabling autostart of your container with system startup.
🛠🔥 Task 3 🛠🔥
Instead of mounting a host directory, you can use a Docker volume. Perform the following steps:
- Create a volume named
my_volume
. - Create and run a container with the volume attached using:
docker run -it --name my_container -v my_volume:/data ubuntu
- Create and save a file inside the container (e.g.,
echo "Hello from Docker!" > /data/hello.txt
). - Exit the container with the
exit
command. - Remove the container with:
docker rm my_container
.
After thinking, check in the console
The following command will automatically remove the container after running and reading the text file:
docker run --rm -v my_volume:/data ubuntu cat /data/hello.txt